package org.xlx.xlx_oa.role_user.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.ResponseBean;
import org.xlx.xlx_oa.role_user.api.AdmUserApi;
import org.xlx.xlx_oa.role_user.domain.dto.AdmUserDto;
import org.xlx.xlx_oa.role_user.domain.dto.SysControlDto;
import org.xlx.xlx_oa.role_user.domain.vo.AdmUserVo;
import org.xlx.xlx_oa.role_user.service.AdmUserService;
import org.xlx.xlx_oa.role_user.service.SysNotificationService;

import java.util.List;

@RestController
@RequestMapping("/v1/adm-user")
public class AdmUserController implements AdmUserApi {
    @Autowired
    private AdmUserService admUserService;
    @Autowired
    private SysNotificationService notificationService;

    @PostMapping("/login")
    @Override
    public ResponseBean checkLogin(@RequestParam(value = "account") String account,
                                   @RequestParam(value = "password") String password) {
        try {
            AdmUserDto admUserDto = admUserService.validateLogin(account, password);
            return ResponseBean.successFactory(admUserDto);
        } catch (BusinessException e) {
            return ResponseBean.failureFactory(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @GetMapping("/logout")
    @Override
    public ResponseBean checkLogout() {
        try {
            return ResponseBean.successFactory(admUserService.validateLogout());
        } catch (BusinessException e) {
            return ResponseBean.failureFactory(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @GetMapping("/getControls/{id}")
    @Override
    public ResponseBean<List<SysControlDto>> getSysControls(@PathVariable(value = "id") Long id) {
        List<SysControlDto> controls = admUserService.getControlsByUserId(id);
        return ResponseBean.successFactory(controls);
    }

    @PostMapping("/save")
    @Override
    public ResponseBean saveEntity(@RequestBody AdmUserVo UserVo) {
        return ResponseBean.successFactory(admUserService.saveEntity(UserVo));
    }

    @DeleteMapping("/remove/{id}")
    @Override
    public ResponseBean removeEntity(@PathVariable(value = "id") Long id) {
        return ResponseBean.successFactory(admUserService.removeEntity(id));
    }

    @PostMapping("/list")
    @Override
    public ResponseBean getList(@RequestBody AdmUserVo UserVo) {
        return ResponseBean.successFactory(admUserService.getList(UserVo));
    }

    @PostMapping("/count")
    @Override
    public ResponseBean getCount(@RequestBody AdmUserVo UserVo) {
        return ResponseBean.successFactory(admUserService.getCount(UserVo));
    }

    @GetMapping("/detail/{id}")
    @Override
    public ResponseBean getDetail(@PathVariable(value = "id") Long id) {
        return ResponseBean.successFactory(admUserService.getDetail(id));
    }

    @PostMapping("/search")
    @Override
    public ResponseBean searchByName(@RequestParam(value = "name") String name) {
        return ResponseBean.successFactory(admUserService.searchByName(name));
    }

    @PostMapping("/icon")
    @Override
    public ResponseBean changeIcon(@RequestPart("file") MultipartFile file) {
        try {
            String upload = notificationService.upload(file);
            return ResponseBean.successFactory(admUserService.updateIcon(upload));
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

}

